

function show(topic, v) {
	var self = this;
	this.v = v;
	this.map={};
	this.number=0;
	this.mimeCodec = 'video/webm;codecs=vp9,opus';
	this.readyBuffer = false;
	this.topic = topic;
	this.createMediaSource = function() {
		if (self.mediaSource) {
			try {
				self.mediaSource.removeSourceBuffer();
			} catch (e) {

			}
			console.log("反初始化成功");
		}
		self.mediaSource = new MediaSource();
		self.mediaSource.addEventListener('sourceopen', function(e) {
			self.sourceBuffer = self.mediaSource
					.addSourceBuffer(self.mimeCodec);
			var curMode = self.sourceBuffer.mode;
			if (curMode == 'segments') {
				self.sourceBuffer.mode = 'sequence';
			}
			self.sourceBuffer.addEventListener('updateend',self.updateend);
			self.readyBuffer = false;
			console.log("初始化成功");
		});
		self.v.src = URL.createObjectURL(self.mediaSource);
	}
	
	this.updateend=function(){
		delete self.map[number+""];
		self.number++;
		if(self.map[self.number+""]!=undefined){
			try {
				self.sourceBuffer.appendBuffer(self.map[self.number+""]);
			} catch (e) {
				console.log(e);
				websocket.send(topic + "-toCollection", JSON.stringify({
					"command" : "createMediaRecorder"
				}));
			}
		}else{
			console.log("空的");
			self.readyBuffer = false;
		}
	}

	this.interval = setInterval(function() {
		try {
			var end = self.v.buffered.end(0);
			var curr = self.v.currentTime;
			console.log(end, curr);
			if ((end - curr) > 1) {
				console.log("清除延迟");
				setTimeout(function() {
					self.v.currentTime = end;
				}, 500);
			}
		} catch (e) {
		}
	}, 1000);

	this.readMsg = function(msg) {
		// 字节消息（视频流）
		if (msg.isByte) {
			console.log(msg.headers.number,self.map);
			console.log("recv video bytes "+msg.body.byteLength);
			self.map[msg.headers.number]=msg.body;
			if(self.number==0&&msg.headers.number!=0){
				websocket.send(topic + "-toCollection", JSON.stringify({
					"command" : "createMediaRecorder"
				}));
				return;
			}
			if(!self.readyBuffer){
				self.number--;
				self.readyBuffer=true;
				self.updateend();
			}
		} else {
			var data = JSON.parse(msg.body);
			console.log(data);
			switch (data.command) {
			case "createMediaSource":
				self.map={};
				self.number=0;
				self.createMediaSource();
				break;
			}
		}
	}

	// 订阅目标的消息
	websocket.subscribe(topic + "-toShow", this.readMsg);
	websocket.subscribe(topic, self.readMsg);
	return this;
}
